﻿using BusinessManage.Common;
using CPFrameWork.Global;
using CPFrameWork.UIInterface.Form;
using CPFrameWork.Utility;
using CPFrameWork.Utility.DbOper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace BusinessManage.ConfirmSign
{
   public class ConfirmRecieve
    {
        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="taskId">任务编号</param>
        /// <param name="orderId">--订单编号</param>
        /// <param name="shop_id">--店铺id</param>
        /// <param name="user_id">--客户id</param>
        /// <param name="createUserId">操作者编号，用于记录日志</param>
        /// <param name="createUserName">操作者姓名，用于记录日志</param>
        /// <returns></returns>
        public string SynRecieve(string taskId="",string orderId="",string shop_id="",string user_id="", string createUserId = "",string createUserName="")
        {
            //1、获取url、token值、时间戳
            string OrderUrl = CPUtils.Configuration.GetSection("AppSettings")["Url"];
            string tokenId = CPUtils.Configuration.GetSection("AppSettings")["TokenId"];
            string currentTimeStamp = TimeStamp.GetTheTimeStamp(true, DateTime.Now);
            //2、参数
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("tid", orderId);              
            sParaTemp.Add("shop_id", shop_id);               //shop_id为3
            sParaTemp.Add("user_id", user_id);              
            sParaTemp.Add("timestamp", currentTimeStamp);
            sParaTemp.Add("method", "trade.confirm");
            sParaTemp.Add("format", "json");
            sParaTemp.Add("sign_type", "MD5");
            sParaTemp.Add("v", "v1");
            //3、调取生产签名的算法  得到sign值
            string signString = "";
            signString = CreateSign.CreateSignString(sParaTemp, tokenId);

            SortedDictionary<string, string> allPara = new SortedDictionary<string, string>();
            allPara = sParaTemp;
            allPara.Add("sign", signString);
            //得到序列化的参数值,拼接成"key1=value1&key2=value2"形式
            string data = BusinessManage.Common.wphttppost.datapost(allPara);

            DbHelper dbHelper = new DbHelper("CPYKWL", CPAppContext.CurDbType());
            //4、获取接口中返回的数据
            string result = "";
            try
            {
                result = BusinessManage.Common.wphttppost.Post(data, OrderUrl, 6);
                //-------从接口获取数据成功，写入日志
                //5、解析数据，记录日志
                JObject joOrdersResult = (JObject)JsonConvert.DeserializeObject(result);
                //标识正确
                if (joOrdersResult["errorcode"].ToString().Equals("0"))
                {                   
                    string OperateDescribe = createUserName+ "在" + DateTime.Now.ToString() +"对任务"+ taskId + "、订单" + orderId + "进行了确认收货操作，确认收货成功。";          
                    string strSql = " INSERT INTO Sys_OperateOrderLog ";
                    strSql += " (OrderId,TaskId,OperateType,OperateName,OperUrl,ParamValue,BeforeContent,AfterContent,OperateDescribe,OperateUserId,OperateUserName,OperateTime) ";
                    strSql += " values('" + orderId + "'";
                    strSql += ",'"+ taskId + "'";
                    strSql += ",''";
                    strSql += ",'确认收货'";
                    strSql += ",''";
                    strSql += ",''";    
                    strSql += ",''";
                    strSql += ",''";
                    strSql += ",'"+OperateDescribe+"'";
                    strSql += ",'"+createUserId+"'";
                    strSql += ",'" + createUserName + "'";
                    strSql += ",'" + DateTime.Now.ToString() + "'";
                    strSql += ");";
                    StringBuilder sb = new StringBuilder();
                    sb.Append(strSql);
                    if (sb.Length > 0)
                    {
                        dbHelper.ExecuteNonQuery(sb.ToString());
                    }
                }
                //标识错误
                else if (joOrdersResult["errorcode"].ToString().Equals("1"))
                {
                    JToken errorJ = joOrdersResult["error"];
                    String message = errorJ["message"].ToString();
                    string OperateDescribe = createUserName + "在" + DateTime.Now.ToString() + "对任务" + taskId + "、订单" + orderId + "进行了确认收货操作，确认收货失败,原因是"+ message;       
                    string strSql = " INSERT INTO Sys_OperateOrderLog ";
                    strSql += " (OrderId,TaskId,OperateType,OperateName,OperUrl,ParamValue,BeforeContent,AfterContent,OperateDescribe,OperateUserId,OperateUserName,OperateTime) ";
                    strSql += " values('" + orderId + "'";
                    strSql += ",'" + taskId + "'";
                    strSql += ",''";
                    strSql += ",'确认收货'";
                    strSql += ",''";
                    strSql += ",''";
                    strSql += ",''";
                    strSql += ",''";
                    strSql += ",'" + OperateDescribe + "'";
                    strSql += ",'" + createUserId + "'";
                    strSql += ",'" + createUserName + "'";
                    strSql += ",'" + DateTime.Now.ToString() + "'";
                    strSql += ");";
                    StringBuilder sb = new StringBuilder();
                    sb.Append(strSql);                    
                    if (sb.Length > 0)
                    {
                        dbHelper.ExecuteNonQuery(sb.ToString());
                    }
                }

            }
            catch (Exception ex)
            {
                //-------从接口获取数据失败，写入日志
                string OperateDescribe = createUserName + "在" + DateTime.Now.ToString() + "对任务" + taskId + "、订单" + orderId + "进行了确认收货操作，从接口获取数据失败。";     
                string strSql = " INSERT INTO Sys_OperateOrderLog ";
                strSql += " (OrderId,TaskId,OperateType,OperateName,OperUrl,ParamValue,BeforeContent,AfterContent,OperateDescribe,OperateUserId,OperateUserName,OperateTime) ";
                strSql += " values('" + orderId + "'";
                strSql += ",'" + taskId + "'";
                strSql += ",''";
                strSql += ",'确认收货'";
                strSql += ",''";
                strSql += ",''";
                strSql += ",''";
                strSql += ",''";
                strSql += ",'" + OperateDescribe + "'";
                strSql += ",'" + createUserId + "'";
                strSql += ",'" + createUserName + "'";
                strSql += ",'" + DateTime.Now.ToString() + "'";
                strSql += ");";
                StringBuilder sb = new StringBuilder();
                sb.Append(strSql);
                if (sb.Length > 0)
                {
                    dbHelper.ExecuteNonQuery(sb.ToString());
                }
                return "从接口获取数据失败 ：" + ex.Message;
            }

            return "确认收货成功";
        }
   }
}
